package com.jy.common.tool;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.jy.entity.GoodsStyle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 公共工具
 *
 * @author fzg
 */
@Slf4j
public class CommonTool {
    /**
     * mapList转为beanList
     *
     * @param mapList       map列表
     * @param beanClass     Bean类型
     * @param isToCamelCase 是否下划线转驼峰
     * @param copyOptions   自定义复制类
     * @return List<T>
     */
    public static <T> List<T> mapListToBeanList(List<Map<String, Object>> mapList, Class<T> beanClass, boolean isToCamelCase, CopyOptions copyOptions) {
        //准备结果
        List<T> list = new ArrayList<>();
        //并行流转换
        mapList.parallelStream().forEachOrdered(item -> list.add(BeanUtil.mapToBean(item, beanClass, isToCamelCase, copyOptions)));
        //返回结果
        return list;
    }

    /**
     * mapList转为beanList
     *
     * @param mapList       map列表
     * @param beanClass     Bean类型
     * @param isToCamelCase 是否下划线转驼峰
     * @return List<T>
     */
    public static <T> List<T> mapListToBeanList(List<Map<String, Object>> mapList, Class<T> beanClass, boolean isToCamelCase) {
        //准备结果
        List<T> list = new ArrayList<>();
        //并行流转换
        mapList.parallelStream().forEachOrdered(item -> list.add(BeanUtil.mapToBean(item, beanClass, isToCamelCase, null)));
        //返回结果
        return list;
    }

    /**
     * mapList转为beanList(默认下划线转驼峰)
     *
     * @param mapList map列表
     * @param beanClass javaBean
     * @return List<T>
     */
    public static <T> List<T> mapListToBeanList(List<Map<String, Object>> mapList, Class<T> beanClass) {
        //准备结果
        List<T> list = new ArrayList<>();
        //并行流转换
        mapList.parallelStream().forEachOrdered(item -> list.add(BeanUtil.mapToBean(item, beanClass, true, null)));
        //返回结果
        return list;
    }

    /**
     * MultipartFile转base64
     *
     * @param file 文件
     * @return String
     * @throws Exception 异常
     */
    public static String test(MultipartFile file) throws Exception {
        BASE64Encoder bEncoder=new BASE64Encoder();
        String[] suffix = Objects.requireNonNull(file.getOriginalFilename()).split("\\.");
        String prefFix="data:image/jpg;base64,".replace("jpg", suffix[suffix.length - 1]);
        return prefFix + bEncoder.encode(file.getBytes()).replaceAll("[\\s*\t\n\r]", "");
    }


    /**
     * 通过请求获取用户aid
     * @param request 请求
     * @return userAid
     */
    public static Integer getUserAidByToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        return JwtUtil.getTokenId(token);
    }

    public static String convertNumberToString(Integer number) {
        if (number >= 10000) {
            double convertedNumber = number / 10000.0;
            return String.format("%.2fW", convertedNumber);
        }
        return number.toString();
    }


    /**
     *
     * @param productStyles 商品款式的list
     * @return List<Map<String, List<String>>>
     */
    public static List<Map<String, Object>> convertToFrontendFormat(List<GoodsStyle> productStyles) {
            List<Map<String, Object>> result = new ArrayList<>();
            if (productStyles!= null &&!productStyles.isEmpty()) {
                // 将所有style_name数据合并后按逗号分割成数组
                String[] allStyleNames = productStyles.stream()
                        .map(GoodsStyle::getStyleName)
                        .findFirst()
                        .orElse("")
                        .split(",");

                for (String styleName : allStyleNames) {
                    HashSet<String> uniqueStyleItems = new HashSet<>();
                    Map<String, Object> styleGroup = new HashMap<>();
                    for (GoodsStyle style : productStyles) {
                        String[] styleValueArray = style.getStyleValue().split(",");
                        for (int i = 0; i < allStyleNames.length; i++) {
                            if (styleName.equals(allStyleNames[i])) {
                                uniqueStyleItems.add(styleValueArray[i].trim());
                            }
                        }
                    }
                    List<String> styleItemsList = new ArrayList<>(uniqueStyleItems);
                    styleGroup.put("styleName", styleName.trim());
                    styleGroup.put("styleItens", styleItemsList);
                    result.add(styleGroup);
                }
            }
            return result;
    }


    public static String formatNumber(int number) {
        if (number > 10000) {
            double result = (double) number / 10000;
            // 使用 String.format 方法将结果保留两位小数
            return String.format("%.2f万", result);
        } else {
            return String.valueOf(number);
        }
    }


}
